package com.kehutong.api.action;


import com.kehutong.common.Authutil.OAuth2Provider;
import com.kehutong.common.DoveClient;
import com.kehutong.common.errcode.Result;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.oauth2.OAuth2Accessor;
import org.coraframework.oauth2.OAuth2ProblemException;
import org.coraframework.util.Objects;

/**
 * TODO
 * 同步第三方小区
 * @author zhouweipeng
 * @version 1.0
 * @date 2021/9/9 16:20
 */
@MvcAction
@ReqMapping("/oau-kht/thridCommunity")
public class ThridCommunityAction {
    @Inject
    private OAuth2Provider oauth2Provider;

    @Inject
    private DoveClient doveClient;

    @ReqMapping("/batchSaveCommunity")
    public Object batchSaveCommunity(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.put("companyNo",companyNo);
        JSONObject result = doveClient.post("/basic/thridCommunity/service/batchThridCommunity", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (result.getIntValue("code") == 200) {
            return Result.success();
        }else{
            return Result.error(500, "系统异常!");
        }
    }
    @ReqMapping("/batchSaveCommunityRelation")
    public Object batchSaveCommunityRelation(JSONObject params) throws Exception {
        String access_token = params.getString("access_token");
        if(Objects.isEmpty(access_token)){
            return Result.error(1003, "token不能为空");
        }
        OAuth2Accessor accessor = null ;
        try {
            accessor = oauth2Provider.getAccessorByAccessToken(access_token);
        } catch (OAuth2ProblemException e){
            return Result.error(1002, "授权已失效");
        }
        String companyNo = (String)accessor.getProperty("companyNo");
        if(Objects.isEmpty(companyNo)){
            return Result.error(1004, "公司编号不能为空!");
        }
        params.put("companyNo",companyNo);
        JSONObject result = doveClient.post("/basic/thridCommunity/service/batchThridCommunityRelation", (http) -> {
            http.addHeader("companyNo", companyNo);
            http.setBody(params.toJSONString());
        });
        if (result.getIntValue("code") == 200) {
            return Result.success();
        }else{
            return Result.error(500, "系统异常!");
        }
    }
}
